using System;
using System.Xml;
using System.IO;
using System.Diagnostics;

namespace RunJob
{
	/// <summary>
	/// Summary description for Class1.
	/// </summary>
	class RunJob
	{
		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main(string[] args)
		{
			if (args.Length==1)
			{
				RunJob app = new RunJob();
				app.Run(args);
			}
			else
			{
				Console.WriteLine("\nRunJob.exe - command line utility for managing MIIS Run Profiles.");
				Console.WriteLine("Usage:");
				Console.WriteLine("\tRunJob.exe <xml_job_file>\n");
				Console.WriteLine("  e.g.");
				Console.WriteLine("\tRunjob.exe run_cycle.xml\n");
			}
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="args"></param>
		public void Run(string[] args)
		{
			EventLog.WriteEntry("RunJob",string.Format("Starting execution of RunJob {0}",args[0]),EventLogEntryType.Information);
			//-- Load the source XML document
			System.Xml.XmlDocument src_xml = null;
			try
			{
				src_xml = new XmlDocument();
				src_xml.Load(args[0]);
			}
			catch (Exception ex)
			{
				Console.WriteLine(ex.Message);
				Console.WriteLine(ex.StackTrace);
				return;
			}

			//-- Now Create the Task List
			TaskList tasks = new TaskList();
			if (tasks.loadFromXML(src_xml))
			{
				if (tasks.execute())
				{
					//-- Task successfully executed
					EventLog.WriteEntry("RunJob",string.Format("successfully completed execution of RunJob {0}",args[0]),EventLogEntryType.Information);
					return;
				}
			}
			EventLog.WriteEntry("RunJob",string.Format("failed execution of RunJob {0}",args[0]),EventLogEntryType.Warning);
		}
	}
}
